package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.FragmentAdapter;
import zj.com.app.ZjApplication;
import zj.com.fragments.DqrJcxFragment;
import zj.com.fragments.JcdInfoFragment;
import zj.com.fragments.JcxFragment;
import zj.com.model.JcxDB;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 * 待确认任务编辑页面
 */
public class DqrEditActivity extends FragmentActivity implements View.OnClickListener{


    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv;

    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private int screenWidth;
    private ViewPager mPageVp;
    /**
     * Tab显示内容TextView
     */
    private TextView mjcxTv, mjcdTv;
    /**
     * Tab的那个引导线
     */
    private ImageView mTabLineIv;

    private  Fragment jcdInfoFragment,dqrJcxFragment;
    private int currentIndex;
    public static List<JcxDB> jcxList;
    public static String remark;

    public LinearLayout id_tab_chat_ll,id_tab_friend_ll;

    public String inspectItemId = "";
    public String accId = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dqr_edit);
        initView();
        init();
        initTabLineWidth();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.getInstance().error("=======onresume=========");
    }

    public void initView(){

        id_tab_chat_ll = (LinearLayout) findViewById(R.id.id_tab_chat_ll);
        id_tab_chat_ll.setOnClickListener(this);
        id_tab_friend_ll = (LinearLayout) findViewById(R.id.id_tab_friend_ll);
        id_tab_friend_ll.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("检查单信息");

        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("确认");
        right_tv.setOnClickListener(this);

        mjcdTv = (TextView) this.findViewById(R.id.id_jcd_tv);
        mjcxTv = (TextView) this.findViewById(R.id.id_jcx_tv);

        mTabLineIv = (ImageView) this.findViewById(R.id.id_tab_line_iv);

        mPageVp = (ViewPager) this.findViewById(R.id.id_page_vp);

        inspectItemId =  getIntent().getStringExtra("InspectionID");
        accId =  getIntent().getStringExtra("accId");

    }

    private void init() {
        jcdInfoFragment = new JcdInfoFragment();

        JcdInfoFragment.inspectItemId = inspectItemId;
        JcdInfoFragment.isDqr = true;
        dqrJcxFragment = new DqrJcxFragment();
        mFragmentList.add(jcdInfoFragment);
        mFragmentList.add(dqrJcxFragment);

        mFragmentAdapter = new FragmentAdapter(
                this.getSupportFragmentManager(), mFragmentList);
        mPageVp.setAdapter(mFragmentAdapter);
        mPageVp.setCurrentItem(0);

        mPageVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }

            /**
             * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
             * offsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset,
                                       int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();

                Log.e("wxf", "postion=" + position + "    offset=" + offset + " =====currentIndex=" + currentIndex);

                if (currentIndex == 0 && position == 0)// 0->1
                {
//                    lp.leftMargin = (int) (offset * (screenWidth * 1.0) + currentIndex
//                            * (screenWidth / 2));
                    lp.leftMargin =
                             0;
                    Log.e("wxf","0  0");


                }else if(currentIndex ==1 && position ==0){
                    Log.e("wxf","1  0");
                    lp.leftMargin =
                             screenWidth / 2;
                }else if(currentIndex ==1 && position ==1){

                    Log.e("wxf","1  1");
                    lp.leftMargin =
                            screenWidth / 2;
                }
              // mTabLineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();
                switch (position) {
                    case 0:
                        mjcdTv.setTextColor(getResources().getColor(R.color.color_text_p));

                        lp.leftMargin =0;
                        break;
                    case 1:


                        lp.leftMargin =
                                screenWidth / 2;
                        mjcxTv.setTextColor(getResources().getColor(R.color.color_text_p));
                             ;
                        break;

                }
                mTabLineIv.setLayoutParams(lp);
                currentIndex = position;
            }
        });

    }



    /**
     * 设置滑动条的宽度为屏幕的1/3(根据Tab的个数而定)
     */
    private void initTabLineWidth() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                .getLayoutParams();
        lp.width = screenWidth / 2;
        mTabLineIv.setLayoutParams(lp);
    }

    /**
     * 重置颜色
     */
    private void resetTextView() {
        mjcxTv.setTextColor(Color.parseColor("#828282"));
        mjcdTv.setTextColor(Color.parseColor("#828282"));
    }






    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                if(ZjApplication.isAcc == 0){
                    doPQr(accId);
                }else{
                    doQr();
                }

                break;
            case R.id.id_tab_chat_ll:

                mPageVp.setCurrentItem(0);
                break;
            case R.id.id_tab_friend_ll:
                mPageVp.setCurrentItem(1);

                break;
        }
    }

    public ProgressDialog dialog;
    public void doQr(){

        if(ZjApplication.checkUser == null){
            Toast.makeText(DqrEditActivity.this,"请选择整改人!",Toast.LENGTH_SHORT).show();

            return;
        }
        if( ZjApplication.dqrJcdInfo != null)
            dialog = DialogUtil.createProgressDialog(this, "提示", "确认操作中...");
        NetManager.getInstance(this).doConfirmInspection(ZjApplication.dqrJcdInfo.getServerInpsectId() + "", new OnResponseListener<List<JcxDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcxDB> result, int errorCode, String errorMsg) {
                dialog.dismiss();
                if(isOK){

                    ZjApplication.checkUser = null;
                    finish();
                }else{
                    Toast.makeText(DqrEditActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
                Toast.makeText(DqrEditActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
            }
        });
   }

    public ProgressDialog dialog2;

    public void doPQr(String state) {

        dialog2 = DialogUtil.createProgressDialog(this, "提示", "确认中");
        LogUtils.getInstance().error("=====state====="+state);
        NetManager.getInstance(this).ConfirmAccompany(state, new OnResponseListener<String>() {
            @Override
            public void onComplete(boolean isOK, String result, int errorCode, String errorMsg) {

                if (isOK) {


                    Toast.makeText(DqrEditActivity.this, "确认成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
                dialog2.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog2.dismiss();
            }
        });
    }
}
